/* eslint-disable jsdoc/no-restricted-syntax */
"use strict";

class MyEntryDataPlugin1 {
  /**
   * @type {*[]}
   */
  entries = [];

  /**
   * @param {import('webpack').Compiler} compiler
   */
  apply(compiler) {
    // 监听 compiler 的 thisCompilation 钩子，以获取 compilation 对象
    compiler.hooks.thisCompilation.tap("MyEntryDataPlugin", (compilation) => {
      console.log("--- MyEntryDataPlugin: thisCompilation Hook ---");

      // 监听 compilation 的 addEntry 钩子
      // 当新的入口被添加到 compilation 时触发
      compilation.hooks.addEntry.tap(
        "MyEntryDataPlugin",
        (dependency, options) => {
          console.log("--- MyEntryDataPlugin: addEntry Hook ---");
          console.log("Entry (Module):", dependency.category); // 通常是入口模块的路径
          console.log("Options (Name, etc.):", options); // 包含入口的名称等信息

          this.entries.push({
            request: dependency.category,
            name: options.name,
          });
        },
      );
    });

    // 示例：在构建完成后打印收集到的入口
    compiler.hooks.done.tap("MyEntryDataPlugin", (stats) => {
      console.log("--- MyEntryDataPlugin: Done Hook (from addEntry) ---");
      if (this.entries && this.entries.length > 0) {
        console.log("Collected Entries:", this.entries);
      } else {
        console.log(
          "No entries collected via addEntry (may be an issue or no entry point configured).",
        );
      }
    });
  }
}

module.exports.MyEntryDataPlugin1 = MyEntryDataPlugin1;

class MyEntryDataPlugin2 {
  /**
   * @type {*[]}
   */
  entries = [];

  /**
   * @param {import('webpack').Compiler} compiler
   */
  apply(compiler) {
    // 监听 compiler 的 thisCompilation 钩子，以获取 compilation 对象
    compiler.hooks.thisCompilation.tap("MyEntryDataPlugin", (compilation) => {
      console.log("--- MyEntryDataPlugin: thisCompilation Hook ---");

      // 监听 compilation 的 addEntry 钩子
      // 当新的入口被添加到 compilation 时触发
      compilation.hooks.addEntry.tap(
        "MyEntryDataPlugin",
        (dependency, options) => {
          console.log("--- MyEntryDataPlugin: addEntry Hook ---");
          console.log("Entry (Module):", dependency.module); // 通常是入口模块的路径
          console.log("Options (Name, etc.):", options); // 包含入口的名称等信息

          this.entries.push({
            module: dependency.module,
            name: options.name,
          });
        },
      );
    });

    // 示例：在构建完成后打印收集到的入口
    compiler.hooks.done.tap("MyEntryDataPlugin", (stats) => {
      console.log("--- MyEntryDataPlugin: Done Hook (from addEntry) ---");
      if (this.entries && this.entries.length > 0) {
        console.log("Collected Entries:", this.entries);
      } else {
        console.log(
          "No entries collected via addEntry (may be an issue or no entry point configured).",
        );
      }
    });
  }
}

module.exports.MyEntryDataPlugin2 = MyEntryDataPlugin2;
